package com.xiaojun.whut.Service.Impl;

import com.xiaojun.whut.Entity.CardMessage;
import com.xiaojun.whut.Repository.CardMessageRepository;
import com.xiaojun.whut.Service.CardService;
import com.xiaojun.whut.util.JacksonUtil;
import com.xiaojun.whut.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CardServiceImpl implements CardService {
    private Logger logger= LoggerFactory.getLogger(getClass());
    @Autowired
    private CardMessageRepository cardRepository;

    @Override
    public List<CardMessage> findAll() {
        return cardRepository.findAll();
    }

    @Override
    public Object save(CardMessage cardMessage) {

        if(cardMessage ==null){
            return ResponseUtil.badArgument();
        }
        try {
            cardRepository.save(cardMessage);
        }catch (Exception e){
            logger.debug(e.getMessage());
            return ResponseUtil.fail(133,e.getMessage());
        }
        return ResponseUtil.ok();
    }

    @Override
    public Object update(String body) {
        System.out.println(body);
        int id= Integer.parseInt(JacksonUtil.parseString(body,"id"));
        int likeNum=Integer.parseInt(JacksonUtil.parseString(body,"likeNum"));
        try {
            CardMessage cardMessage=cardRepository.findById(id);
            cardMessage.setLikeNum(likeNum);
            cardRepository.save(cardMessage);
        }catch (Exception e){
            return ResponseUtil.fail(13,e.getMessage());
        }
        return ResponseUtil.ok();
    }

    @Override
    public Object delete(int id) {
        try {
            CardMessage cardMessage=cardRepository.findById(id);
            cardRepository.delete(cardMessage);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return ResponseUtil.ok();
    }


}
